//
//  LoginManager.m
//  UIControlDevApp
//
//  Created by shenchengcai on 15/3/3.
//  Copyright (c) 2015年 QM. All rights reserved.
//

#import "LoginManager.h"
#import "ShowListModel.h"

@implementation LoginManager

//登录请求方法。向接口发送登录信息并处理返回值。option请求参数，包括用户名和密码；block返回后的处理方法
+ (void)getUsersInfoWithOption:(NSDictionary *)option andBlock:(void (^)(NSDictionary *, NSError *))block
{
    APIManager *manager = [APIManager loginInstance];
    
    [manager postPath:API_USER_LOGIN
           parameters:option
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSDictionary *result = [TCUtils getDictionaryWithUTF8Data:responseObject];
                  //登录成功
                  if ([[result objectForKey:KEY_RESULT] intValue] == 0) {
                      
                      block(@{KEY_DATA:result}, nil);
                      
                  } else {
                      block(nil, nil);
                  }
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"Error: %@", error);
                  block(nil, error);
              }];
}

//下载列表请求方法。向接口发送登录信息并处理返回值。option请求参数，包括用户名和密码；block返回后的处理方法
+ (void)getDownloadListWithOption:(NSDictionary *)option andBlock:(void (^)(NSMutableArray *, NSError *))block
{
    APIManager *manager = [APIManager sharedInstance];
    
    [manager postPath:API_BOOK_LIST
           parameters:option
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSDictionary *result = [TCUtils getDictionaryWithUTF8Data:responseObject];
                  
                  // 返回列表数据
                  if ([[result objectForKey:KEY_RESULT] intValue] == 0) {
                      
                      NSArray *data = [result objectForKey:KEY_DATA];
                      NSMutableArray *returnItems = [NSMutableArray arrayWithCapacity:data.count];
                      [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                          NSDictionary *dic = (NSDictionary *)obj;
                          
                          ShowListModel *item = [ShowListModel objectFromDictionary:dic];
                          [returnItems addObject:item];
                          item = nil;
                      }];
                      block(returnItems, nil);
                      
                  } else {
                      NSLog(@"errorMessage:%@",[result objectForKey:@"msg"]);
                      block(nil, nil);
                  }
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  block(nil, error);
              }];
}

@end
